<?php
/**
 * Notification util
 *
 * PHP version 5
 *
 * @category   Web
 * @package    Tourstart
 * @subpackage Util
 * @author     Alex Slubsky <aslubsky@gmail.com>
 * @license    Tourstart License http://tourstart.dk/license
 * @version    SVN: $Id: $
 *
 *
 */

/**
 * Includes
 */


/**
 * Notification util
 *
 * PHP version 5
 *
 * @category   Web
 * @package    Tourstart
 * @subpackage Search
 * @author     Alex Slubsky <aslubsky@gmail.com>
 * @license    Tourstart License http://tourstart.dk/license
 * @version    Release: $Id: $
 *
 *
 */

class DaemonSheduler extends DaemonAbstract
{
    public function work()
    {
        $tasks = Scheduler::getNotDone();
        foreach($tasks as $task) {
            $this->runTask($task);
        }
    }
    
    protected function runTask(Scheduler $task)
    {
        $task->state = Scheduler::START_TASK;
        $task->save();
        
        switch($task->task) {
            case 'build':
                $this->build($task);
            break;
        }

        $updTask = Scheduler::getById(intval($task->id));
        if($updTask->state == Scheduler::CANCEL_TASK) {
            $updTask->delete();
        } else {
            $task->save();
        }
    }
    
    protected function build(Scheduler $task)
    {
        $comand = 'ant -file /bazalt/configs/build.xml -Dproject='.$task->Project->name.' -Dtestspath='.$task->Project->Settings->tests_path;
        //print $comand;
        exec($comand . " 2>&1", $out);
        $task->result = implode("\n",$out);        
        
        $res = explode(' ',$out[count($out)-2]);
        if($res[1] == 'SUCCESSFUL') {
            $task->state = Scheduler::SUCCESS_TASK;
        } else {
            $task->state = Scheduler::FAILURE_TASK;
        }
    }
}


